import { createWebHistory, createRouter } from "vue-router"
import { Tools } from "../utils/Tools"
const routes = [
    {
        path: '/',
        redirect: '/homepage'
    },
    {
        name: "home",
        path: "/",
        meta: { title: "首页" },
        component: () => import("../views/Home.vue"),
        children: [
            {
                name: "homepage",
                path: "homepage",
                meta: { title: "首页" },
                component: () => import("../views/Home/HomePage.vue"),
            },
            {
                name: "user",
                path: "user",
                meta: { title: "用户" },
                component: () => import("../views/User/User.vue"),
            },
            {
                name: "role",
                path: "role",
                meta: { title: "角色",back:true },
                component: () => import("../views/Role/Role.vue"),
            },
            {
                name: "category",
                path: "category",
                meta: { title: "分类",back:true },
                component: () => import("../views/Category/Category.vue"),
            },
            {
                name: "message",
                path: "message",
                meta: { title: "消息" },
                component: () => import("../views/Message/Message.vue"),
            },
            {
                name: "set",
                path: "set",
                meta: { title: "设置" },
                component: () => import("../views/Set/Set.vue"),
            },
            {
                name: "tb",
                path: "tb",
                meta: { title: "贴吧",back:true },
                component: () => import("../views/Tb/Tb.vue"),
            },
            {
                name: "article",
                path: "article",
                meta: { title: "贴子" ,back:true},
                component: () => import("../views/Article/Article.vue"),
            },
            {
                name: "joke",
                path: "joke",
                meta: { title: "逗逗" ,back:true},
                component: () => import("../views/Joke/Joke.vue"),
            },
        ]
    },
    {
        name: "login",
        path: "/Login",
        meta: { title: "登录" },
        component: () => import("../views/user/Login.vue")
    },
];

const Router = createRouter({
    history: createWebHistory(),
    routes
});

/* 前置路由守卫 */
Router.beforeEach((to, from, next) => {
    if (to.name == "login") {
        next();
    } else {
        console.log(Tools.verifyToken())
        if (!Tools.verifyToken()) {
            Tools.showMessage("请先登录!", "warning");
            next("login")
        } else {
            next();
        }
    }
})

export default Router;